package com.siwei.mes.controller.mixProportion;

import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.mixproportion.MixProportion;
import com.siwei.mes.service.mixProportion.MixProportionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 配合比表相关
 *
 * @author dmwang
 * @since 2024-07-29 21:01:49
 */
@Slf4j
@Api(value = "配合比表")
@RestController
@RequestMapping("mixProportion")
public class MixProportionController {
    @Resource
    private MixProportionService mixProportionService;


    @ApiOperation(value = "新增配合比表")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) MixProportion entity) {
        return Json.succ().data(ApiResult.addColumn("id", mixProportionService.create(entity, true)));
    }

    @ApiOperation(value = "修改配合比表")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) MixProportion entity) {
        mixProportionService.update(entity, true);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除配合比表")
    @PostMapping("/delete")
    public Json delete(@RequestBody MixProportion entity) {
        mixProportionService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取配合比表")
    @PostMapping("/page")
    public Json appPage(@RequestBody PageQuery<MixProportion, MixProportion> query) {
        return Json.succ().data(mixProportionService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取配合比表")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(mixProportionService.loadById(id));
    }

    @ApiOperation(value = "获取所有配合比表")
    @PostMapping("getAll")
    public Json getAll(@RequestBody MixProportion entity) {
        return Json.succ().data(ApiResult.addList(mixProportionService.getAll(entity)));
    }

    @ApiOperation(value = "补偿上传erp/协会")
    @PostMapping("/compensate")
    public Json compensate(@RequestBody MixProportion entity) {
        assertTrue(entity == null || entity.getIds() == null || entity.getIds().isEmpty(), "配合比主键id不能为空！");

        mixProportionService.compensate(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "同步协会配合比历史数据至品控")
    @GetMapping("historyProportion")
    public Json historyProportion() {
        mixProportionService.historyProportion();
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "导入excel配合比数据至品控与协会")
    @GetMapping("importProportion")
    public Json importProportion(@RequestParam("filePath") String filePath) {
        assertTrue(filePath == null || filePath.isEmpty(), "请填写excle所在的目录地址！");
        mixProportionService.importProportion(filePath);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "导入excel配合比数据至品控与协会(新)")
    @GetMapping("importProportionNew")
    public Json importProportionNew(@RequestParam("filePath") String filePath) {
        assertTrue(filePath == null || filePath.isEmpty(), "请填写excle所在的目录地址！");
        mixProportionService.importProportionNew(filePath);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "导出数据至excel文件")
    @GetMapping("exportProportion")
    public Json exportProportion(HttpServletRequest request) {
        String filePath = mixProportionService.exportProportion(request);
        return Json.succ().data(ApiResult.addColumn("filePath", filePath));
    }


    @ApiOperation(value = "前端导入excel配合比数据至品控与协会")
    @PostMapping("frontEndImportProportion")
    public Json frontEndImportProportion(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        if (file.isEmpty()) {
            throw new RuntimeException("excel上传文件不能为空");
        }
        String originalFilename = file.getOriginalFilename();
        assertTrue(StringUtils.isBlank(originalFilename), "请上传excel文件！");
        
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf('.') + 1);

        if (!"xlsx".equalsIgnoreCase(fileExtension) && !"xls".equalsIgnoreCase(fileExtension)) {
            throw new RuntimeException("文件格式不正确，仅支持xlsx或xls格式");
        }
        mixProportionService.frontEndImportProportion(file);
        return Json.succ().msg("导入配合比数据成功！");
    }

    @ApiOperation(value = "因前端计算公式变化 查询出所有配合比数据新公式对不上的数据")
    @GetMapping("queryPhbNoNot")
    @AnonymousAccess
    public Json queryPhbNoNot() {
        return Json.succ().data(mixProportionService.queryPhbNoNot());
    }
}